package itsos.controller;

import itsos.common.ConstCode;
import itsos.model.*;
import itsos.service.IssueBoxService;
import itsos.service.IssueCommentService;
import itsos.service.IssueService;
import itsos.service.MemberService;
import itsos.service.RepositoryService;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class IssueController extends MultiActionController {
	private IssueService issueService;
	
	private RepositoryService repositoryService;
	
	private IssueCommentService issueCommentService;
	
	private IssueBoxService issueBoxService;
	
	private MemberService memberService;
	

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	
	public void setIssueBoxService(IssueBoxService issueBoxService) {
		this.issueBoxService = issueBoxService;
	}
	

	public void setIssueCommentService(IssueCommentService issueCommentService) {
		this.issueCommentService = issueCommentService;
	}

	public void setRepositoryService(RepositoryService repositoryService)
	{
		this.repositoryService = repositoryService;
	}
		
	public void setIssueService(IssueService issueService) {
		this.issueService = issueService;
	}
	
	

	
	public ModelAndView addIssue(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/issue/addIssue");
		Repository repository = new Repository();
		repository = repositoryService.getReposioryByRepositoryID(request);
		
		mav.addObject("repository", repository);
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	public ModelAndView insertIssue(HttpServletRequest request, HttpServletResponse response)
	{
		
		ModelAndView mav = new ModelAndView("workpage/issue/addcomissue");
		issueService.insertIssue(request);
		long a = new Long(Long.parseLong(request.getParameter("issueAttriCode")));
		if(a==ConstCode.REPORT)
		{
			mav.setViewName("workpage/mainissue/introPage");
		}
		mav.addObject("repositoryID",(request.getParameter("repositoryID")));
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	
	
	/*
	public ModelAndView addIssue(HttpServletRequest request, HttpServletResponse response, HttpSession session, Object command) {
		ModelAndView mav = new ModelAndView();
		
		if(!issueService.insertIssue(request))
		{
			mav.setViewName("devpage/errmessage");
			return mav;
		}
		mav.setViewName("devpage/com");
		
		return mav;
	}*/
	
	public ModelAndView addIssueForm(HttpServletRequest request, HttpServletResponse response, HttpSession session, Object command)
	{
		ModelAndView mav = new ModelAndView("devpage/insertIssueform");
		
		
		if(request.getSession().getAttribute("memberID")!=null)
		mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	
	public ModelAndView getIssueListByRepositoryID(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("issueList",issueService.IssueList(request));
		mav.setViewName("devpage/issuelist");
		
		
		if(request.getSession().getAttribute("memberID")!=null)
		mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	public ModelAndView viewProcIssue(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/issue/issueView");
		
		Issue issue;
		issue = issueService.viewProcIssue(request);
		mav.addObject("viewIssue", issue);		
		
		List list = issueCommentService.viewComment(request);
		
		mav.addObject("issueComment", list);
		
		if(request.getSession().getAttribute("memberID")!=null)
		mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	public ModelAndView viewReptIssue(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/issue/issueView");
		
		Issue issue;
		issue = issueService.viewReptIssue(request);
		mav.addObject("viewIssue", issue);	
		List list = issueCommentService.viewComment(request);
		
		mav.addObject("issueComment", list);
		
		if(request.getSession().getAttribute("memberID")!=null)
		mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	public ModelAndView insertComment(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/issue/issueView");
		
		Issue issue;
		issue = issueService.viewReptIssue(request);
				
		mav.addObject("viewIssue", issue);	
		issueCommentService.insertComment(request);
		
		mav.addObject("issueComment" ,issueCommentService.viewComment(request));
		
		if(request.getSession().getAttribute("memberID")!=null)
		mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}

	public ModelAndView insertIssueBox(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/issue/issueView");
		
		Issue issue;
		issue = issueService.viewReptIssue(request);
		mav.addObject("viewReptIssue", issue);	
		List list = issueCommentService.viewComment(request);
		
		
		mav.addObject("issueComment", list);
		
		issueBoxService.insertIssuebox(request);
		
		
		if(request.getSession().getAttribute("memberID")!=null)
		mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}

	
	public ModelAndView editProcessorContent(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/issue/issueView");
		if(request.getParameter("processorContent").equals(null))
		{
			return mav;
		}
		issueService.editProcessorContent(request);
		
		
		Issue issue;
		issue = issueService.viewProcIssue(request);
		mav.addObject("viewIssue", issue);		
		
		List list = issueCommentService.viewComment(request);
		
		mav.addObject("issueComment", list);
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
			mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	public ModelAndView insertProcComment(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/issue/issueView");
		
		Issue issue;
		issue = issueService.viewProcIssue(request);
		mav.addObject("viewIssue", issue);		
		
		List list = issueCommentService.viewComment(request);
		issueCommentService.insertComment(request);
		
		mav.addObject("issueComment", list);
		
		if(request.getSession().getAttribute("memberID")!=null)
		mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	public ModelAndView issueProcToPage(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/issue/issueProcToPage");
		
		mav.addObject("issueID", new Long(Long.parseLong(request.getParameter("issueID"))));
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
			mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}

	public ModelAndView issueProc(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/mainissue/introPage");
		
		issueService.issueProc(request);
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
			mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	public ModelAndView adminProcIssue(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/issue/issueToPage");

		mav.addObject("viewIssue", issueService.viewProcIssue(request));		
		
		if(request.getSession().getAttribute("memberID")!=null)
		mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	public ModelAndView issueAdminToPage(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/issue/adminProcForm");
		
		mav.addObject("issueID", new Long(Long.parseLong(request.getParameter("issueID"))));
		mav.addObject("devMember", memberService.devMember());
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
			mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	public ModelAndView issueToProc(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/mainissue/adminProc");
		
		mav.addObject(issueService.issueToProc(request));
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
			mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	public ModelAndView issueTracking(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/issue/issueTracking");
		mav.addObject("issueHistory", issueService.issueTracking(request));
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
			mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	
}